﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using MobySharp.Results;

namespace MobySharp.Actions
{
    public class AddLike : BaseAction, IAction
    {
        private string _tinyUrlCode;
        private long _postId;

        public AddLike(string tinyURLCode)
        {
            _tinyUrlCode = tinyURLCode;

            RequestType = HTTPRequestType.Post;
            Action = APICall.addLike;
        }

        public AddLike(long postId)
        {
            _postId = postId;

            RequestType = HTTPRequestType.Post;
            Action = APICall.addLike;
        }

        [PostDataInstruction("tinyurl_code", false)]
        public string TinyURLCode
        {
            get { return _tinyUrlCode; }
        }


        [PostDataInstruction("post_id", false)]
        public long PostId
        {
            get { return _postId; }
        }

        /// <summary>
        /// Gets or sets the username. This is optional in combination with the PIN/password.
        /// Likes without username/password will be added anonimously
        /// </summary>
        /// <value>The username.</value>
        [PostDataInstruction("username", false, 32)]
        public string Username { get; set; }

        /// <summary>
        /// Gets or sets the password/pin. This is optional in combination with the PIN/password.
        /// Likes without username/password will be added anonimously
        /// </summary>
        /// <value>The password.</value>
        [PostDataInstruction("pin", false, 32)]
        public string Password { get; set; }

        #region IAction Members

        IResult IAction.ParseResult(string result)
        {
            var r = new LikeResult();
            try
            {
                if (result.Contains("<?xml"))
                {
                    var serializer = new XmlSerializer(r.GetType());
                    r = (LikeResult)serializer.Deserialize(new StringReader(result));

                    return r;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            return r;
        }

        #endregion
    }

}
